package com.example.controller;

import com.example.pojo.Announcement;
import com.example.pojo.Job;
import com.example.pojo.JobInfo;
import com.example.service.AnnouncementService;
import com.example.service.JobInfoService;
import com.example.service.JobService;
import com.example.util.ResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/institute")
public class InstituteController {

    @Autowired
    private JobService jobService;

    @Autowired
    private JobInfoService jobInfoService;

    @Autowired
    private AnnouncementService announcementService;

    @GetMapping("/works")
    public ResponseWrapper<List<Job>> getWorks(){
        List<Job> jobs = jobService.getExamineWorks();
        return ResponseWrapper.success(jobs);
    }

    @GetMapping("/workInfos/{useridentityfer}")
    public ResponseWrapper<List<JobInfo>> getJobInfos(@PathVariable Integer useridentityfer){
        List<JobInfo> jobInfos = jobInfoService.getJobInfos(useridentityfer);
        System.out.println("待审核职位数："+jobInfos.size());
        return ResponseWrapper.success(jobInfos);
    }


    @GetMapping("/reject/{id}/{useridentityfer}/{msg}")
    public ResponseWrapper<Job> rejectOne(@PathVariable String id,@PathVariable Integer useridentityfer,@PathVariable String msg){
        int row = jobService.rejectOne(id,useridentityfer,msg);
        System.out.println("jobId:"+id);
        return ResponseWrapper.success();
    }

    @GetMapping("/access/{id}/{useridentityfer}")
    public ResponseWrapper<Job> accessOne(@PathVariable String id,@PathVariable Integer useridentityfer){
        int row = jobService.accessOne(id,useridentityfer);
        System.out.println("jobId:"+id);
        return ResponseWrapper.success();
    }

    @GetMapping("/rejectAll/{idlist}/{useridentityfer}/{msg}")
    public ResponseWrapper<Job> rejectAll(@PathVariable List<String> idlist,@PathVariable Integer useridentityfer,@PathVariable String msg){
        System.out.println(idlist);
        int row = jobService.updateJobsToReject(idlist);
        jobInfoService.insertRejectListReason(idlist,useridentityfer,msg);
        return ResponseWrapper.success();
    }

    @GetMapping("/accessAll/{idList}/{useridentityfer}")
    public ResponseWrapper<Job> accessAll(@PathVariable List<String> idList,@PathVariable Integer useridentityfer){
        int row = jobService.updateJobsToAccess(idList);
        jobInfoService.insertAccessListReason(idList,useridentityfer);
        System.out.println("jobsId:"+idList);
        return ResponseWrapper.success();
    }

    @GetMapping("/announcements/{month}")
    public ResponseWrapper<List<Announcement>> getAnnouncements(@PathVariable int month){
        List<Announcement> announcementList = announcementService.getAnnouncementListByMonth(month);
        System.out.println("announcementList:"+announcementList);
        return ResponseWrapper.success(announcementList);
    }

    @GetMapping("/completeworks/{useridentityfer}")
    public ResponseWrapper<List<Job>> getCompleteJobs(@PathVariable int useridentityfer){
        List<Integer> idList =  jobInfoService.getCompleteJobInfos(useridentityfer);
        if (idList.isEmpty()){
            List<Job> jobList = new ArrayList<>();
            return ResponseWrapper.success(jobList);
        }
        else {
            List<Job> jobList = jobService.getCompleteJobs(idList);
            return ResponseWrapper.success(jobList);
        }

    }



}
